package com.topvision.camerasdk.camera;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Point;
import android.os.AsyncTask;
import android.provider.Settings;
import android.util.Log;
import android.widget.ImageView;

import com.squareup.picasso.Callback;
import com.squareup.picasso.MemoryPolicy;
import com.squareup.picasso.Picasso;
import com.topvision.camerasdk.R;
import com.topvision.camerasdk.album.ShowPicActivity;
import com.topvision.camerasdk.album.sensor.MyTransformation;
import com.topvision.commomsdk.uitl.AndroidUtils;
import com.topvision.commomsdk.uitl.TvDeviceUtil;

import java.io.File;
import java.lang.ref.WeakReference;


/**
 * 异步加载图片
 *
 * @author jack
 *         create at 2016/11/22 17:10
 */
public class BitmapWorkerTask extends AsyncTask<String, Void, String> {
    private final WeakReference<ImageView> imageViewReference;
    //目标文件地址
    private String data = null;
    Context context;
    Point point;


    public BitmapWorkerTask(Context context, ImageView imageView) {
        // Use a WeakReference to ensure the ImageView can be garbage
        // collected
        imageViewReference = new WeakReference<>(imageView);
        this.context = context;
        point = AndroidUtils.getScreenPoint(context);
        int width = point.x;
        int height = point.y;
        Log.e("tvLog", "width=" + width + ",height=" + width);

    }

    // Decode image in background.
    @Override
    protected String doInBackground(String... params) {
        data = params[0];
        return data;
    }

    // Once complete, see if ImageView is still around and set bitmap.
    @SuppressLint("NewApi")
    @Override
    protected void onPostExecute(String data) {
//        Picasso.with(context).load(new File(data)).fit()
//                .centerCrop().memoryPolicy(MemoryPolicy.NO_CACHE, MemoryPolicy.NO_STORE).config(Bitmap.Config.RGB_565).error(R.mipmap.pic_fail).into(imageViewReference.get());
//        Picasso.with(context).load(new File(data)).memoryPolicy(MemoryPolicy.NO_CACHE, MemoryPolicy.NO_STORE).error(R.mipmap.pic_fail)
//                .into(imageViewReference.get());
        File imageFile = new File(data);
        if (imageFile != null && imageFile.exists() && imageFile.length() > 0) {
//            Picasso.with(context).load(new File(data)).memoryPolicy(MemoryPolicy.NO_CACHE, MemoryPolicy.NO_STORE).error(R.mipmap.pic_fail)
//                    .into(imageViewReference.get());
            int resizeWidth = point.x + 2 * context.getResources().getDimensionPixelSize(R.dimen.image_shift) * 2;
            int resizeHeight = point.y + 2 * context.getResources().getDimensionPixelSize(R.dimen.image_shift) * 2;
            Log.e("tvLog", "resizeWidth=" + resizeWidth + ",resizeHeight=" + resizeHeight);
//            Bitmap bitmap = BitmapFactory.decodeFile(imageFile.getPath());
            ShowPicActivity.isloading = true;
            Picasso.with(context)
                    .load(imageFile)
                    .memoryPolicy(MemoryPolicy.NO_CACHE, MemoryPolicy.NO_STORE)
                    .error(R.mipmap.pic_fail)
                    .config(Bitmap.Config.RGB_565)
                    .resize(1024, 768)
                    .centerCrop()
                    .transform(new MyTransformation(imageViewReference.get()))
                    .into(imageViewReference.get(), new Callback() {
                        @Override
                        public void onSuccess() {
                            ShowPicActivity.isloading = false;
                        }

                        @Override
                        public void onError() {
                            ShowPicActivity.isloading = false;
                        }
                    });
        }
    }

}